package org.jimmy.tang.ehcache.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jimmy.tang.ehcache.Account;
import org.jimmy.tang.ehcache.better.AccountService;
import org.jimmy.tang.spring.utils.SpringContextUtils;

public class EhcacheServlet extends HttpServlet {

    private static final long serialVersionUID = -4601399782894132232L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        System.out.println("doPost");
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("doGet");
        AccountService service = (AccountService) SpringContextUtils
                .getBean("accountServiceBean1");
        // simpleTest(service);
        // evictTest(service);
        // conditionTest(service);
        // groupTest(service);
        // putTest(service);
        // customTest(service);
        // thisTest(service);
        // evictFailTest(service);
        // beforeInvocationTest(service);
        ehcacheTest(service);
    }

    /**
     * 
     * @param service
     */
    protected void simpleTest(AccountService service) {

        // 第一次查询，应该走数据库
        System.out.print("first query...");
        service.getAccountByName("somebody");
        // 第二次查询，应该不查数据库，直接返回缓存的值
        System.out.print("second query...");
        service.getAccountByName("somebody");
        System.out.println();
    }

    /**
     * 
     * @param service
     */
    protected void evictTest(AccountService service) {
        // 第一次查询，应该走数据库
        System.out.print("first query...");
        service.getAccountByName("somebody");
        // 第二次查询，应该不查数据库，直接返回缓存的值
        System.out.print("second query...");
        service.getAccountByName("somebody");
        System.out.println();

        System.out.println("start testing clear cache...");
        // 更新某个记录的缓存，首先构造两个账号记录，然后记录到缓存中
        Account account1 = service.getAccountByName("somebody1");
        service.getAccountByName("somebody2");
        // 开始更新其中一个
        account1.setId(1212);
        service.updateAccount(account1);
        service.getAccountByName("somebody1");// 因为被更新了，所以会查询数据库
        service.getAccountByName("somebody2");// 没有更新过，应该走缓存
        service.getAccountByName("somebody1");// 再次查询，应该走缓存
        // 更新所有缓存
        service.reload();
        service.getAccountByName("somebody1");// 应该会查询数据库
        service.getAccountByName("somebody2");// 应该会查询数据库
        service.getAccountByName("somebody1");// 应该走缓存
        service.getAccountByName("somebody2");// 应该走缓存

    }

    /**
     * 
     * @param service
     */
    protected void conditionTest(AccountService service) {
        service.getAccountByName("somebody");// 长度大于 4，不会被缓存
        service.getAccountByName("sbd");// 长度小于 4，会被缓存
        service.getAccountByName("somebody");// 还是查询数据库
        service.getAccountByName("sbd");// 会从缓存返回
    }

    /**
     * 
     * @param service
     */
    protected void groupTest(AccountService service) {
        service.getAccount("somebody", "123456", true);// 应该查询数据库
        service.getAccount("somebody", "123456", true);// 应该走缓存
        service.getAccount("somebody", "123456", false);// 应该走缓存
        service.getAccount("somebody", "654321", true);// 应该查询数据库
        service.getAccount("somebody", "654321", true);// 应该走缓存
    }

    /**
     * 
     * @param service
     */
    protected void putTest(AccountService service) {
        Account account = service.getAccountByName("someone");
        account.setPassword("123");
        service.updateAccount1(account);
        account.setPassword("321");
        service.updateAccount1(account);
        account = service.getAccountByName("someone");
        System.out.println(account.getPassword());
    }

    /**
     * 
     * @param service
     */
    protected void customTest(AccountService service) {
        Account account = service.getAccountByName("someone");
        System.out.println("passwd=" + account.getPassword());
        account = service.getAccountByName("someone");
        System.out.println("passwd=" + account.getPassword());
    }

    /**
     * 
     * @param service
     */
    protected void thisTest(AccountService service) {
        service.getAccountByName2("someone");
        service.getAccountByName2("someone");
        service.getAccountByName2("someone");
    }

    /**
     * 
     * @param service
     */
    protected void evictFailTest(AccountService service) {
        service.getAccountByName("someone");
        service.getAccountByName("someone");
        try {
            service.reload1();
        } catch (Exception e) {
        }
        service.getAccountByName("someone");
    }

    /**
     * 
     * @param service
     */
    protected void beforeInvocationTest(AccountService service) {
        service.getAccountByName("someone");
        service.getAccountByName("someone");
        try {
            service.reload2();
        } catch (Exception e) {
        }
        service.getAccountByName("someone");
    }

    /**
     * 
     * @param service
     */
    protected void ehcacheTest(AccountService service) {
        // 第一次查询，应该走数据库
        System.out.print("first query...");
        service.getAccountByName("som");
        // 第二次查询，应该不查数据库，直接返回缓存的值
        System.out.print("second query...");
        service.getAccountByName("som");
        System.out.println();

        System.out.println("start testing clear cache...");
        // 更新某个记录的缓存，首先构造两个账号记录，然后记录到缓存中
        Account account1 = service.getAccountByName("somebody1");
        service.getAccountByName("somebody2");
        // 开始更新其中一个
        account1.setId(1212);
        service.updateAccount(account1);
        service.getAccountByName("somebody1");// 因为被更新了，所以会查询数据库
        service.getAccountByName("somebody2");// 没有更新过，应该走缓存
        service.getAccountByName("somebody1");// 再次查询，应该走缓存
        // 更新所有缓存
        System.out.println();
        service.reload();
        service.getAccountByName("somebody1");// 应该会查询数据库
        service.getAccountByName("somebody2");// 应该会查询数据库
        service.getAccountByName("somebody1");// 应该走缓存
        service.getAccountByName("somebody2");// 应该走缓存
    }

}
